/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.agency;

import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.LoggedData.LoggedData;
import researchgrants.parts.LoggedData.LoggedDataOutSerializable;
import researchgrants.parts.db.Db;

/**
 * Represent a Contact within an Agency. Hence the name: AgencyContact.
 * Each Agency can have several AgencyContacts.
 * Each AgencyContact belong to a specific Agency
 * 
 * The class is LoggedDataOutSerializable to allow it to be saved implicitly through panels
 * This object is a field within a GrantRequest
 * @author DOStudent1
 */
public class AgencyContact implements LoggedDataOutSerializable{

    // Creates a new AgencyContact
    static AgencyContact inputNew(Window parent, int agencyId) {
        // initializes an unbound Agency Contact View - to input the new AgencyContact
        AgencyContactView viewNew = new AgencyContactView(parent, agencyId);
        // Returns the newly created AgencyContact
        AgencyContact newAgencyContact=viewNew.getAgencyContact();
        return (newAgencyContact);
    }

    // Returns an existing AgencyContact
    static AgencyContact getById(int id) {
        return(new AgencyContact(id));
    }

    // the identification field of the AgencyContact
    private int id;
    
    // The elements of the AgencyContact
    private LoggedData name;
    private LoggedData phone;
    private LoggedData email;
    private LoggedData address;
    private LoggedData notes;
    private int agencyId;
    
    // private constructor
    private AgencyContact(int id) {
        this.id=id;
        syncDataFromDb();
    }

    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT NameRef, PhoneRef, EmailRef, AddressRef, NotesRef, AgencyId FROM tblAgencyContacts WHERE ContactID=?");
            prepareStatement.setInt(1, id);
            ResultSet agencyResultSet = prepareStatement.executeQuery();
            Db.moveToFirstLine(agencyResultSet);

            name=LoggedData.getByID((Integer) Db.getColumn(agencyResultSet, 1));
            phone=LoggedData.getByID((Integer) Db.getColumn(agencyResultSet, 2));
            email=LoggedData.getByID((Integer) Db.getColumn(agencyResultSet, 3));
            address=LoggedData.getByID((Integer) Db.getColumn(agencyResultSet, 4));
            notes=LoggedData.getByID((Integer) Db.getColumn(agencyResultSet, 5));
            agencyId=(Integer) Db.getColumn(agencyResultSet, 6);
                    
            agencyResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(AgencyContact.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AgencyContact.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //////////////////////////////////////
    // Retreives the elements' values
    //////////////////////////////////////

    public int getId() {
       return(id);
    }

    public String getCurrentName() {
        return(name.value());
    }

    public String getCurrentPhone() {
        return(phone.value());
    }

    public String getCurrentEmail() {
        return(email.value());
    }

    public String getCurrentAddress() {
        return(address.value());
    }

    public String getCurrentNotes() {
        return(notes.value());
    }

    public Agency getCurrentAgency() {
        return(Agency.getById(agencyId));
    }

    //////////////////////////////////////
    // Retreives the raw elements
    // Used to populate the panels to display the AgencyContact in AgencyContactView
    //////////////////////////////////////

    LoggedData getLoggedDataName() {
        return(name);
    }

    LoggedData getLoggedDataPhone() {
        return(phone);
    }

    LoggedData getLoggedDataEmail() {
        return(email);
    }

    LoggedData getLoggedDataAddress() {
        return(address);
    }

    LoggedData getLoggedDataNotes() {
        return(notes);
    }

    
    
    // Used to summarize the AgencyContact
    public String getDescription() {
        return (name.value());
    }
    
    // Shows the AgencyContact
    void show(Window parent) {
        new AgencyContactView(parent, this);
    }
 
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Support saving and loading a AngecyContact through its panels: LoggedDataPanelsListExpnesePurpose
    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    @Override
    public String toLoggedData() {
        return(Integer.toString(id));
    }

    public static AgencyContact fromLoggedData(String loggedData) {
        return(AgencyContact.getById(Integer.parseInt(loggedData)));
    }

    
}
